﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net;

namespace Common
{
    public delegate string FunOutput(DateTime dt); //根据一个 DateTime 返回一个输出字符串

    public class DateHelper
    {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 获取秒数 T8
        /// </summary>
        public static string T8
        {
            get { return DateTime.Now.ToString("yyyyMMdd"); }
        }

        /// <summary>
        /// 获取秒数 T14
        /// </summary>
        public static string T14
        {
            get { return DateTime.Now.ToString("yyyyMMddhhmmss"); }
        }

        /// <summary>
        /// 获取毫秒数 T17
        /// </summary>
        public static string T17
        {
            get { return DateTime.Now.ToString("yyyyMMddhhmmssfff"); }
        }

        #region ** 获取当前日期是第几周 **
        public static int WeekIndex(DateTime curDay)
        {
            try
            {
                int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "- " + "1-1 ").DayOfWeek);
                int days = curDay.DayOfYear;
                int daysOutOneWeek = days - (7 - firstdayofweek);

                if (daysOutOneWeek <= 0)
                {
                    return 1;
                }
                else
                {
                    int weeks = daysOutOneWeek / 7;
                    if (daysOutOneWeek % 7 != 0)
                        weeks++;

                    return weeks + 1;
                }
            }
            catch (System.Exception ex)
            {
                Log.Error("Common.SysDate >  WeekIndex() 出错\r\n" + ex.Message);
                throw new Exception(ex.Message);
            }
        }
        public static int WeekIndex(string date)
        {
            DateTime curDay = Convert.ToDateTime(date);
            return WeekIndex(curDay);
        }
        #endregion


        #region ** 是否为日期 **
        private static bool IsDateTime(string sDate)
        {
            try
            {
                Convert.ToDateTime(sDate);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion


        #region ** 两个日期相减 **
        /// <summary>
        /// 时间相减，取得TimeSpan dt2-dt1
        /// </summary>
        public static TimeSpan TimeDiff(DateTime dt1, DateTime dt2)
        {
            try
            {
                TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();

                return ts;
            }
            catch (System.Exception ex)
            {
                Log.Error("Common.SysDate > TimeDiff() 出错\r\n" + ex.Message);
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 日期相减，取得日 dt2-dt1
        /// </summary>
        public static int DateDiff(DateTime dt1, DateTime dt2)
        {
            TimeSpan ts = TimeDiff(dt1, dt2);
            return ts.Days;
        }
        #endregion


        #region ** 今天周几 **
        public static string ChinaWeek(DateTime dt)
        {
            string[] sDay = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            return sDay[WeekNum(dt)];
        }
        public static int WeekNum(DateTime dt)
        {
            return Convert.ToInt16(dt.DayOfWeek);
        }
        #endregion


        #region ** 日历表 **
        public static string DateTable(DateTime sDate, DateTime eDate, FunOutput fun)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                int dLength = DateDiff(sDate, eDate);

                //遍历时间段 
                DateTime theDate = sDate;
                List<DateTime> li = new List<DateTime>();
                for (int i = 0; i < dLength; i++)
                {
                    if (i > 0) theDate = theDate.AddDays(1);
                    li.Add(theDate);
                }

                //判断是否跨周，从第二个项目开始循环
                int iWeek = 1;
                for (int i = 1; i < li.Count; i++)
                {
                    if ((WeekIndex(li[i]) - WeekIndex(li[i - 1])) > 0) iWeek++;
                }

                //生成表头
                sb.Append("<table width=\"100%\">");

                //循环生成表格
                for (int tr = 0; tr < iWeek; tr++)
                {
                    //生成行
                    sb.Append("<tr>");

                    //生成列 
                    for (int td = 0; td < 7; td++)
                    {
                        sb.Append("<td>");

                        if (li.Count > 0)
                        {
                            for (int n = 0; n < li.Count; n++)
                            {
                                if (Convert.ToInt32(li[n].DayOfWeek) == td)
                                {
                                    DateTime dt = li[n];

                                    //使用一个委托的方法获取输出
                                    string strOut = fun(dt);
                                    sb.Append(strOut);

                                    li.Remove(li[n]);
                                    break;
                                }
                                else
                                {
                                    sb.Append("&nbsp;"); break;
                                }
                            }
                        }
                        else
                        {
                            sb.Append("&nbsp;");
                        }
                        sb.Append("</td>");
                    }

                    sb.Append("</tr>");
                }

                sb.Append("</table>");
                return sb.ToString();
            }
            catch (System.Exception ex)
            {
                Log.Error("Common.SysDate > DateTable() 出错\r\n" + ex.Message);
                throw new Exception(ex.Message);
            }
        }
        #endregion

    }
}
